package com.soft.mainsite.module.web.system;

import com.soft.mainsite.common.utils.StrUtil;
import com.soft.mainsite.common.annotation.FormToken;
import com.soft.mainsite.common.constant.CmsConst;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.common.utils.ControllerUtil;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.web.cms.service.CategoryService;
import com.soft.mainsite.module.web.cms.service.ContentService;
import com.soft.mainsite.module.web.cms.service.TopicService;
import com.soft.mainsite.module.web.system.service.SysUserService;
import com.soft.mainsite.module.web.system.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Description:系统控制器
 *
 * @author Jin
 * @create 2017-04-14
 **/
@Controller
public class SystemController {


    @Autowired
    private SysUserService userService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private CategoryService categoryService;


    @Autowired
    private TopicService topicService;

    private static final String PARAM_SUCCESS = "success";

    /**
     * 后台首页地址
     *
     * @return
     */
    @RequestMapping("/system")
    public String index() {
        return "system/index";
    }


    /**
     * 后台登陆地址
     *
     * @return
     */
    @FormToken
    @RequestMapping("/${system.login.path}/login")
    public String login() {
        Subject currentUser = SecurityUtils.getSubject();
        if (!CmsUtil.isNullOrEmpty(currentUser) && currentUser.isAuthenticated()) {
            return "redirect:/system";
        }
        return "system/login";
    }

    @RequestMapping("/${system.login.path}/logout")
    public String logout(@Value("${system.login.path}") String adminLoginPath) {
        Subject currentUser = SecurityUtils.getSubject();
        if (!CmsUtil.isNullOrEmpty(currentUser) && currentUser.isAuthenticated()) {
            currentUser.logout();
        }
        return "redirect:/" + adminLoginPath + "/login";
    }


    /**
     * 后台登陆提提交地址
     *
     * @param username
     * @param password
     * @param remberMe
     * @return
     */

    @FormToken
    @RequestMapping("/${system.login.path}/doLogin")
    @ResponseBody
    public Map<String, Object> doLogin(
            HttpServletRequest request,
            @RequestParam(value = "verifyCode", required = false) String verifyCode,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "password", required = false) String password,
            @RequestParam(value = "remberMe", required = false, defaultValue = "") String remberMe) {

        // 临时验证码验证
        if (StrUtil.isBlank(verifyCode) || !ControllerUtil.validate(verifyCode, request)) {
            return JsonUtil.toMap(false, "验证码输入错误");
        }
        return userService.login(request, username, password, remberMe);

    }

    @FormToken
    @RequestMapping("/ajax/doLogin")
    @ResponseBody
    public String doLogin(
            HttpServletRequest request,
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam(value = "remberMe", required = false, defaultValue = "") String remberMe) {
        Map resutl = userService.login(request, username, password, remberMe);
        if ((Boolean) resutl.get(PARAM_SUCCESS)) {
            return JsonUtil.toSuccess("登陆成功", "", true);
        }
        return JsonUtil.toError("登陆失败！");
    }

    @FormToken
    @RequestMapping("/loginTimeout")
    public String doTimeOut() {
        return "system/login_timeout";
    }


    /**
     * 修改密码
     *
     * @return
     */
    @RequestMapping("/system/changePassword")
    public String changePassword(HttpSession session, Model model) {
        UserVo userVo = (UserVo) session.getAttribute(CmsConst.SITE_USER_SESSION_KEY);
        if (CmsUtil.isNullOrEmpty(userVo)) {
            throw new UnauthenticatedException();
        }
        model.addAttribute("userId", userVo.getUserId());
        return "system/change_password";
    }

    @RequestMapping("/system/doChangePassword")
    @ResponseBody
    public String doChangePassword(@RequestParam("userId") Integer userId, @RequestParam("oldPassword") String op, @RequestParam("password") String np) {
        return userService.changePassword(userId, op, np);
    }

    @RequestMapping("/system/welcome")
    public String welcome(Model model) {
        model.addAttribute("contentCount", contentService.allCount());
        model.addAttribute("categoryCount", categoryService.allCount());
        model.addAttribute("topicCount", topicService.allCount());
        model.addAttribute("userCount", userService.countAll());
        return "system/welcome";
    }

    @RequestMapping("/system/AllMonthCharts")
    @ResponseBody
    public String charts() {
        return contentService.findAllMonthCount();
    }
}
